﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace RescueRobotController.Panels
{
    public partial class AttitudePanel : DockContent
    {
        private float xAngle = 0;
        private float yAngle = 0;
        Pen hud_p;
        Graphics g;
        private Thread drawHUDThread = null;
        private bool drawHUDThreadRunning = true;
        // 下面的内容是每个面板必须要有的
        // *********** BEGIN ************* //
        // “发送AT指令”事件
        public delegate void ExistATCmdToSendHandler(ATCmd cmd);
        public event ExistATCmdToSendHandler SendATCmd;

        // 处理AT指令
        public void HandleATCmd(ATCmd cmd)
        {
            string target;
            float floatValue;

            target = cmd.target;
            floatValue = float.Parse(cmd.value);

            if (target == "CHASSIS_ANGLE_X") xAngle = floatValue;
            if (target == "CHASSIS_ANGLE_Y") yAngle = floatValue;
        }

        private void DrawHUD(Graphics hud_g)
        {
            float yc;
            float delta_yc;
            float y0;
            float y1;
            float y2;
            float yGnd;

            yc = pictureBox1.Height / 2;
            delta_yc = (float)(0.5 * pictureBox1.Height * yAngle / 90.0);
            y0 = yc - delta_yc;
            y1 = (float)(pictureBox1.Height / 2 + (0.5 * pictureBox1.Width * Math.Tan(xAngle / 180.0 * Math.PI)));
            y2 = (float)(pictureBox1.Height / 2 - (0.5 * pictureBox1.Width * Math.Tan(xAngle / 180.0 * Math.PI)));

            // 绘制水平中间线
            hud_p.Width = 2;
            hud_p.Color = Color.Gray;
            hud_g.DrawLine(hud_p, 0, pictureBox1.Height / 2, pictureBox1.Width, pictureBox1.Height / 2);
            
            // 绘制水平参考线
            yGnd = yc + delta_yc;
            hud_g.DrawLine(hud_p, 0, yGnd, pictureBox1.Width, yGnd);

            // 填充天空和大地的颜色
            hud_g.FillRectangle(new SolidBrush(Color.Brown), 0, yGnd, pictureBox1.Width, pictureBox1.Height - yGnd);
            hud_g.FillRectangle(new SolidBrush(Color.Blue), 0, 0, pictureBox1.Width, yGnd);

            // 绘制地平线
            hud_p.Width = 5;
            hud_p.Color = Color.Green;
            hud_g.DrawLine(hud_p, 0, y1, pictureBox1.Width, y2);
            hud_g.DrawArc(hud_p, pictureBox1.Width / 2-10, pictureBox1.Height / 2-10, 20, 20, 0, 360);
        }

        public AttitudePanel()
        {
            InitializeComponent();
            hud_p = new Pen(Color.Green) { Width = 5 };
        }

        private void DrawHUDThread()
        {
            while (drawHUDThreadRunning)
            {
                pictureBox1.BeginInvoke(new Action(() =>
                {
                    g = pictureBox1.CreateGraphics();
                    // g.Clear(Color.White);
                    DrawHUD(g);
                }));
                Thread.Sleep(20);
            }
        }

        private void AttitudePanel_Load(object sender, EventArgs e)
        {
            DoubleBuffered = true;
            g = pictureBox1.CreateGraphics();
            drawHUDThread = new Thread(DrawHUDThread);
            drawHUDThread.IsBackground = true;
            drawHUDThread.Start();
        }

        private void AttitudePanel_FormClosing(object sender, FormClosingEventArgs e)
        {
            drawHUDThreadRunning = false;
        }
    }
}
